﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecisionTree
{
    static class ShiftedDBCreater
    {
        private static byte[][] CreateDB(byte[][] inputs)
        {
            byte[][] rVal = new byte[inputs.Length * 5][];

            for (int i = 0; i < inputs.Length; i++)
            {
                byte[][] images = new byte[5][];

                images[0] = inputs[i];
                images[1] = Shift(inputs[i], 0, -2);
                images[2] = Shift(inputs[i], -2, 0);
                images[3] = Shift(inputs[i], 0, 2);
                images[4] = Shift(inputs[i], 2, 0);

                for (int j = 0; j < images.Length; j++)
                {
                    rVal[i * images.Length + j] = images[j];
                }
            }

            return rVal;
        }

        private static byte[] Shift(byte[] input, int v, int h)
        {
            byte[,] image = PyramidCreater.Convert2D(input);
            byte[,] rVal = new byte[image.GetLength(0), image.GetLength(1)];

            for (int i = 0; i < rVal.GetLength(0); i++)
            {
                for (int j = 0; j < rVal.GetLength(1); j++)
                {
                    int ii = i + h;
                    int jj = j + v;

                    if (ii > -1 && jj > -1 && ii < rVal.GetLength(0) && jj < rVal.GetLength(1)) // if it's in the image
                    {
                        rVal[ii, jj] = image[i, j];
                    }
                }
            }

            return PyramidCreater.Convert1D(rVal);
        }

        static void Dowork()
        {
            byte[][] inputs = FileOperations.ReadImages(Parameters.TrainInputPath);

            byte[][] newDB = ShiftedDBCreater.CreateDB(inputs);

            DBWriter.WriteDB(newDB, "C:\\x5Inputs2.data");

            byte[] outputs = FileOperations.ReadLabels(Parameters.TrainOutputPath);
            byte[] newOutputs = new byte[outputs.Length * 5];

            for (int i = 0; i < outputs.Length; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    newOutputs[i * 5 + j] = outputs[i];
                }
            }

            DBWriter.WriteLabels(newOutputs, "C:\\x5Outputs2.data");
        }
    }
}
